package com.example.ui.other;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.service.MainService;
import com.example.ui.share.SeeActivity;
import com.example.util.Util;

public class ChangePasswordActivity extends Activity implements OnClickListener{
	private Button leftBtn,rightBtn,changePasswordBtn;
	private TextView titleText;
	private EditText originalEdit,newEdit,confirmEdit;
	private String result,data;
	String str1,str2,str3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other_change_password);
		init();
	}

	private void init() {
		leftBtn=(Button)findViewById(R.id.layout_title_left_btn);
		rightBtn=(Button)findViewById(R.id.layout_title_right_btn);
		titleText=(TextView)findViewById(R.id.layout_title_middle_text);
		leftBtn.setText("返回");
		rightBtn.setVisibility(View.INVISIBLE);
		titleText.setText("修改密码");
		originalEdit=(EditText)findViewById(R.id.other_change_password_original_edit1);
		newEdit=(EditText)findViewById(R.id.other_new_password_edit2);
		confirmEdit=(EditText)findViewById(R.id.other_confirm_new_password_edit3);
		changePasswordBtn=(Button)findViewById(R.id.change_password_btn);
		leftBtn.setOnClickListener(this);
		changePasswordBtn.setOnClickListener(this);
	}
	Runnable sumbit_run = new Runnable() {

		@Override
		public void run() {
		
			data = NetRequest.postRequestChangePassword(str1,str2);
			result = AnalyzingJson.return_iaresult(data);
			h.sendEmptyMessage(2);
		}
	};
	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 2:
				if (result!=null&&"true".equals(result)) {
					Util.toast(ChangePasswordActivity.this, "密码修改成功，再次登陆生效");
					MainService.isAutoLogin=false;
					finish();
				} else {
					Util.toast(ChangePasswordActivity.this, "提交失败，请重试");
				}

				break;
			default:
				break;
			}

		};

	};
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_title_left_btn:
			finish();
			break;
		case R.id.change_password_btn:
			if(info()){
				 new Thread(sumbit_run).start();
			
			}
			
			break;

		default:
			break;
		}
		
	}

	private boolean info() {
		 str1 = originalEdit.getText().toString();
		 str2 = newEdit.getText().toString();
		 str3 = confirmEdit.getText().toString();
		if("".equals(str1)||"".equals(str2)||"".equals(str3)){
			Util.toast(this, "请确认新密码");
			return false;
		}else if(!str2.equals(str3)){
			Util.toast(this, "请确认新密码");
			return false;
		}
		return true;
	}

}
